package cn.wyf.androidcode.di.modules;

import com.google.gson.Gson;
import com.google.gson.JsonElement;

import net.wyf.library.request.DownloadListener;
import net.wyf.library.request.FileCallback;
import net.wyf.library.request.Requester;

import java.util.List;
import java.util.Map;

import cn.wyf.androidcode.application.CodeContast;
import cn.wyf.androidcode.bean.DataCollect;
import cn.wyf.androidcode.bean.FirstVideo;
import cn.wyf.androidcode.bean.Info;
import cn.wyf.androidcode.bean.User;
import cn.wyf.androidcode.di.api.RequestApiService;
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 网络请求管理类，基于Request对retrofit的二次封装
 */

public class RequestHelper {
    private volatile static RequestHelper instance = null;

    private RequestHelper() {
    }

    public static RequestHelper getInstance() {
        if (instance == null) {
            synchronized (RequestHelper.class) {
                if (instance == null) instance = new RequestHelper();
            }
        }
        return instance;
    }

    private static RequestApiService requestAPI;

    /**
     * 初始化网络请求
     */
    public void init(boolean isDebug) {
        Requester.initBuilder(isDebug);
        requestAPI = Requester.getRequestAPI(CodeContast.HOST_URL, RequestApiService.class);
    }

    /**
     * 请求登录
     *
     * @param params 请求参数
     * @return
     */
    public Observable<User> doLogin(Map<String, String> params) {
        return requestAPI.requestLogin(params)
                .map(new Function<User, User>() {
                    @Override
                    public User apply(User user) throws Exception {
                        return user;
                    }
                });
    }

    public Observable<List<FirstVideo>> doFirstVideo(String url){
        return requestAPI.requestFirstVideo(url)
                .map(new Function<List<FirstVideo>, List<FirstVideo>>() {
                    @Override
                    public List<FirstVideo> apply(List<FirstVideo> firstVideos) throws Exception {
                        return firstVideos;
                    }
                });
    }

    public Observable<List<Info>> doInfo(){
        return requestAPI.requestInfo()
                .map(new Function<List<Info>, List<Info>>() {
                    @Override
                    public List<Info> apply(List<Info> jsonElement) throws Exception {
                        return jsonElement;
                    }
                });
    }

    /**
     * 下载文件
     *
     * @param url      下载地址
     * @param filePath 文件存储路径
     * @param listener 下载监听
     */
    public Call downloadFile(String url, String filePath, DownloadListener listener) {
        Call<ResponseBody> responseBodyCall = requestAPI.requestGetCall(url);
        responseBodyCall.enqueue(new FileCallback(url, filePath, listener));
        return responseBodyCall;
    }

    public Observable<DataCollect> doGetData(String url){
        return requestAPI.requestGetCall2(url)
                .map(new Function<JsonElement, DataCollect>() {
                    @Override
                    public DataCollect apply(JsonElement jsonElement) throws Exception {
                        DataCollect dataCollect = new Gson().fromJson(jsonElement, DataCollect.class);
                        return dataCollect;
                    }
                });
    }
}
